package com.daona.security.user;

import com.daona.security.token.Token;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User implements UserDetails {

  private Integer id;
  private String firstname;
  private String lastname;
  private String email;
  private String password;

  private Role role;

  private List<Token> tokens;

  /**
   * 获取用户的权限
   * 这里是根据角色枚举的权限来获取的(静态的而非从数据库动态读取)
   * @return 用户权限列表
   */
  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return role.getAuthorities();
  }

  @Override
  public String getPassword() {
    return password;
  }

  @Override
  public String getUsername() {
    return email;
  }

  /**
   * 账号是否未过期
   * @return true 未过期
   */
  @Override
  public boolean isAccountNonExpired() {
    return true; //todo
  }

  /**
   * 账号是否未锁定
   * @return true 未锁定
   */
  @Override
  public boolean isAccountNonLocked() {
    return true; //todo
  }

  /**
   * 密码是否未过期
   * @return true 未过期
   */
  @Override
  public boolean isCredentialsNonExpired() {
    return true; //todo
  }

  /**
   * 账号是否激活
   * @return true 已激活
   */
  @Override
  public boolean isEnabled() {
    return true; //todo
  }
}
